package com.link.base.base.wechat.controller;

import com.link.base.base.wechat.model.AutoReply;
import com.link.base.base.wechat.model.AutoReplys;
import com.link.base.base.wechat.service.AutoReplyService;
import com.link.base.wechat.basic.model.InputMessage;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 自动回复Controller ，用于创建、查询、修改配置的自动回复规则
 *
 * @author lujiang
 * @date 2018-05
 */
@Controller
@RequestMapping("/link/autoReply")
public class AutoReplyController extends BasicController<AutoReply> {

    @Resource
    private AutoReplyService autoReplyService;

    @Override
    public BasicService<AutoReply> getBasicService() throws Exception {
        return autoReplyService;
    }

    /**
     * 分页查询自动回复规则
     *
     * @author lujiang
     * @date 2018-05
     * @param autoReply 自动回复对象
     * @return
     */
    @RequestMapping(value = "/QueryautoReply", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryautoReply(AutoReply autoReply) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        try {
            map = autoReplyService.queryautoReply(autoReply);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    /**
     * 新增自动回复
     *
     * @author lujiang
     * @date 2018-05
     * @param autoReplys 自动回复对象
     * @return
     */
    @RequestMapping(value = "/insertAutoReply", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> insertAutoReply(@RequestBody AutoReplys autoReplys) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        try {
            map = autoReplyService.insertAutoReply(autoReplys);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    /**
     * 删除自动回复
     *
     * @author lujiang
     * @date 2018-05
     * @param autoReply 自动回复对象id
     * @return
     */
    @RequestMapping(value = "/deleteAutoReply", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteAutoReply(@RequestBody AutoReply autoReply) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        try {
            map = autoReplyService.deleteAutoReply(autoReply.getId());
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    /**
     * 查看自动回复
     *
     * @author lujiang
     * @date 2018-05
     * @param id  自动回复对象id
     * @return
     */
    @RequestMapping(value = "/queryAutoReplyById", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryAutoReplyById(Long id) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        try {
            map = autoReplyService.queryAutoReplyById(id);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    /**
     * 修改自动回复
     *
     * @author lujiang
     * @date 2018-05
     * @param autoReplys 自动回复对象
     * @return
     */
    @RequestMapping(value = "/updateAutoReplyById", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> updateAutoReplyById(@RequestBody AutoReplys autoReplys) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        try {
            map = autoReplyService.updateAutoReplyById(autoReplys);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }


    /**
     * 微信回调处理，提供成接口，给微信工程调用
     *
     * @author 黄龙建
     * @date 2020年06月10日
     * @param inputMessage 自动回复对象
     * @return map
     */
    @RequestMapping(value = "/processWxRequest", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> processWxRequest(@RequestBody InputMessage inputMessage) {
        Map<String, Object> map = new HashMap<>(16);
        try {
            String ret  = autoReplyService.processWxRequest(inputMessage);
            map.put("success", true);
            map.put("result", ret);
        } catch (Exception e) {
            map.put("success", false);
            map.put("result", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }
}
